package com.animal.controller;
import com.animal.entity.pojo.AGoodsy;
import com.animal.entity.pojo.AUser;
import com.animal.service.GoodsyService;
import com.animal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("goodsy")
public class GoodsyController {

    @Autowired
    private UserService userService;
    @Resource
    private GoodsyService goodsyService;

    @RequestMapping("/getAGoodsyAll")
    public String getAGoodsyAll(Model model) {
        List<AGoodsy> list = goodsyService.getAGoodsyAll();
        if (list != null) {
            model.addAttribute("aglist", list);
            return "goods";
        } else {
            return "error";
        }
    }


    @RequestMapping(value = "/updateAGoodsy",method = RequestMethod.POST)
    public String updateAGoodsy(@RequestParam("id") Integer id,
                                @RequestParam("salvageamount") Integer salvageamount, HttpServletRequest request) {
        AGoodsy aGoodsy = goodsyService.getAGoodsyByid(id);
        int salvageamount2 = aGoodsy.getSalvageamount()+salvageamount;
        boolean flag = goodsyService.updateSalvageamountById(id, salvageamount2);
        if (flag) {
            AUser aUser = new AUser();
            aUser.setId(id);
            aUser.setScore(salvageamount);
            HttpSession session = request.getSession(true);
            AUser user = (AUser) session.getAttribute("user");
            userService.updateScore(aUser);
            return "redirect:/goodsy/getAGoodsyAll";
        } else {
            return "error";
        }
    }
}
